package jmine.tec.persist.schema.impl.parsers;

import java.util.regex.Matcher;

import jmine.tec.persist.schema.impl.elements.AlternateKeyElement;
import jmine.tec.persist.schema.impl.elements.Schema;

/**
 * Parser de alternate keys definidos de forma anônima no meio de um create table.
 * 
 * @author lundberg
 */
public class AlternateKeyTableParser extends AbstractSchemaElementParser<AlternateKeyElement> {

    private static final int AK_COLUMNS_GROUP_INDEX = 1;

    /**
     * Construtor
     * @param schema schema
     */
    public AlternateKeyTableParser(Schema schema) {
        super("unique\\s+\\(([\\w\\s,]+)\\)", schema);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected AlternateKeyElement doParse(Matcher matcher) {
        AlternateKeyElement ak = new AlternateKeyElement(getSchema());
        for (String columnName : matcher.group(AK_COLUMNS_GROUP_INDEX).replaceAll("\\s", "").split(",")) {
            ak.addColumn(columnName);
        }
        return ak;
    }
}
